<!-- components/UserProfile.vue -->
<template>
  <div v-if="authStore.isAuthenticated" class="user-profile">
    <a-avatar size="large" :style="{ backgroundColor: '#1890ff' }">
      {{ authStore.currentUser?.username?.charAt(0)?.toUpperCase() }}
    </a-avatar>
    <div class="user-info">
      <div class="username">{{ authStore.currentUser?.username }}</div>
      <div class="email">{{ authStore.currentUser?.email }}</div>
    </div>
    <a-button type="link" @click="handleLogout" class="logout-btn">退出</a-button>
  </div>

  <div v-else class="login-prompt">
    <div class="auth-buttons">
      <a-button type="primary" @click="navigateTo('/login')" size="small">登录</a-button>
      <a-button @click="navigateTo('/register')" size="small">注册</a-button>
    </div>
  </div>
</template>

<script setup lang="ts">
// 确保正确导入 useAuthStore
import { useAuthStore } from '~/stores/auth'

const authStore = useAuthStore()

const handleLogout = () => {
  authStore.clearAuth()
  navigateTo('/login')
}
</script>

<style scoped>
.user-profile {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  border-radius: 8px;
  border: 1px solid #e8e8e8;
  min-width: 280px;
}

.user-info {
  flex: 1;
  min-width: 0;
}

.username {
  font-weight: bold;
  font-size: 14px;
  color: #262626;
  margin-bottom: 2px;
}

.email {
  color: #8c8c8c;
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.logout-btn {
  color: #ff4d4f;
  font-size: 12px;
  padding: 0;
  height: auto;
}

.logout-btn:hover {
  color: #ff7875;
}

.login-prompt {
  display: flex;
  align-items: center;
}

.auth-buttons {
  display: flex;
  gap: 8px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .user-profile {
    min-width: auto;
    padding: 6px 8px;
  }

  .username {
    font-size: 12px;
  }

  .email {
    font-size: 10px;
  }

  .auth-buttons {
    flex-direction: column;
    gap: 4px;
  }
}
</style>